<?php

namespace App\Models\Teaching;

use App\Models\System\Term;
use App\Models\System\User;
use Illuminate\Database\Eloquent\Model;

class Live extends Model
{
    protected $table = 'lives';
    protected $guarded = [];
    protected $appends = [
        'host_set',
    ];
    
    public function term()
    {
        return $this->hasOne(Term::class, 'id', 'data_version_id');
    } 
    
    //创建人
    public function creator()
    {
        return $this->hasOne(User::class, 'id', 'creator_id');
    }  

    public function getHostSetAttribute(){
        $host = !empty($this->attributes['host']) ? $this->attributes['host'] : '';

        if(!$host){
            return [];
        }

        $hosts = explode(',', $host);
        $users = User::select('id', 'nickname')->whereIn('id', $hosts)->get()->toArray();
        
        $result['hosts'] = $hosts;
        $result['host_users'] = $users;

        return $result;
    }
}
